<?php defined('SYSPATH') or die('No direct script access.');

class Model_Platform_Listener extends ORM
{
    public static function order_listener($order_sn)
    {
        try {
            $order = Model_Member_Order::order_info($order_sn);
            if (! $order)
            {
                throw new Exception('订单不存在');
            }
            if (!in_array($order['status'],array(2,5)))
            {
                throw new Exception('非已支付状态');
            }
            if ($order['payprice'] <= 0)
            {
                throw new Exception('支付金额小于或等于0');
            }
            //$member = Model_Member::get_member_byid($order['memberid']);
            $model = Model_Model::get_module_info($order['typeid']);
            $arr = array(
                'ordersn'   => $order['ordersn'],
                'type_id'   => $order['typeid'],
                'amount'    => $order['payprice'],
                'mid'       => $order['memberid'],
                'pdt_type'  => $model['modulename'],
                'pdt_name'  => $order['productname'],
                'pay_time'  => $order['paytime'],
                'pay_type'  => $order['paysource'],
                'pay_num'   => $order['online_transaction_no'],
                'pay_proof' => $order['payment_proof'],
                'salesman'  => $order['saleman'],
                'linkman'   => $order['linkman'],
                'linktel'   => $order['linktel'],
                'status'    => 0,
            );

            if (!Model_Platform_Income::save_info($arr)) {
                throw new Exception('添加收入记录失败');
            }
            $res = [
                'status' => true,
                'msg'    => '添加收入记录成功'
            ];
        } catch (Exception $e) {
            $res = [
                'status' => false,
                'msg'    => $e->getMessage()
            ];
        }

        return $res;
    }
}